#include <stdio.h>

#include "ebm_env.h"
#include "nstar_light_glint.h"

#include "user_cfg.h"





unsigned short ebmEnv_get_plat_port(void) {return p_readcfg->ebm.plat_port;}
unsigned int ebmEnv_get_passback_ip(void) {return p_readcfg->ebm.passback_ip;}
unsigned short ebmEnv_get_passback_port(void) {return p_readcfg->ebm.passback_port;}


unsigned char ebmEnv_get_vol(void) {return p_readcfg->vol;}
unsigned char ebmEnv_get_volmax(void) {return p_readcfg->vol_max;}
unsigned char ebmEnv_fm_tp_get(void) {return p_readcfg->ebm.fm_tp;}
unsigned char ebmEnv_getphyid(unsigned char* physicsBuf) { memcpy(physicsBuf, read_serid(), 6);\
                                                                 return 1;}
unsigned int ebmEnv_getfmfq(void) {return p_readcfg->ebm.fm_fq;}
unsigned char ebmEnv_setfmfq(unsigned int val) { return set_val_u32(val, &p_wcfg->ebm.fm_fq); }

unsigned char ebmEnv_set_vol(unsigned char vol)
{
	unsigned char ret= usercfg_set_vol(vol);
	return ret;
}

unsigned char ebmEnv_set_ip(unsigned char *ip)
{
	unsigned int ip32, mask, gw;
	ip32= *((unsigned int*)ip);
	mask= *((unsigned int*)(ip+4));
	gw= *((unsigned int*)(ip+8));
	ip32= Swap32(ip32);
	mask= Swap32(mask);
	gw= Swap32(gw);
	printf("ip= %x, mask =%x, gateway=%x\n", ip32, mask, gw);
	return 1;
}

unsigned int ebmEnv_get_sip(void)
{
    return p_readcfg->ebm.plat_ip;
}


unsigned char ebmEnv_set_platip(unsigned char *ip, unsigned short port)
{
#define usercfg_set_platip(val) set_val_u32(val, &p_wcfg->ebm.plat_ip)
#define usercfg_set_platport(val) set_val_u16(val, &p_wcfg->ebm.plat_port)  
	unsigned char ret;
	unsigned int ip_32= *((unsigned int*)ip);
	ip_32= Swap32(ip_32);
	ret= usercfg_set_platip(ip_32);
	ret|= usercfg_set_platport(port);
	if(ret != 0){
		adp_cmdNet_start();
	}
	return 1;
}

unsigned char ebmEnv_set_pgip(unsigned char *ip, unsigned short port)
{
#define usercfg_set_pgip(val) set_val_u32(val, &p_wcfg->ebm.pg_ip)
#define usercfg_set_pgport(val) set_val_u16(val, &p_wcfg->ebm.pg_port)  
	unsigned char ret;
	unsigned int ip_32= *((unsigned int*)ip);
	ip_32= Swap32(ip_32);
	ret= usercfg_set_pgip(ip_32);
	ret|= usercfg_set_pgport(port);
	if(ret != 0){
		adp_pgNet_start();
	}
	return 1;
}

unsigned char ebmEnv_set_backip(unsigned char *ip, unsigned short port)
{
#define usercfg_set_backip(val) set_val_u32(val, &p_wcfg->ebm.passback_ip)
#define usercfg_set_backport(val) set_val_u16(val, &p_wcfg->ebm.passback_port)  
	unsigned char ret;
	unsigned int ip_32= *((unsigned int*)ip);
	ip_32= Swap32(ip_32);
	ret= usercfg_set_backip(ip_32);
	ret|= usercfg_set_backport(port);
	if(ret != 0){
		adp_cmdNet_start();
	}
	return 1;
}

#define MAX_TP (60*10)
unsigned char ebmEnv_set_back_tp(unsigned short val)
{
#define usercfg_set_passback_tp(val) set_val_u16(val, &p_wcfg->ebm.passback_tp)      
	if(val > MAX_TP)
		val= MAX_TP;
	return usercfg_set_passback_tp(val);
}

unsigned short ebmEnv_get_back_tp(void)
{
	return p_readcfg->ebm.passback_tp;
}

unsigned char ebmEnv_set_heart_tp(unsigned short val)
{
#define usercfg_set_heart_tp(val) set_val_u16(val, &p_wcfg->ebm.heart_tp)      
    if(val > MAX_TP)
        val= MAX_TP;
    return usercfg_set_heart_tp(val);
}

unsigned short ebmEnv_get_heart_tp(void)
{
    unsigned short tp = p_readcfg->ebm.heart_tp;
    tp = MIN(tp, 10);
    if(tp == 0) {
        tp = 2;
    }
    return tp;
}

unsigned char ebmEnv_set_fm_tp(unsigned short val)
{
#define usercfg_set_fm_tp(val) set_val_u8(val, &p_wcfg->ebm.fm_tp)      
    return usercfg_set_fm_tp(val);
}


unsigned char ebmEnv_set_logicID(unsigned char *sourceid)
{
#define usercfg_set_sourceid(val) set_val_array(p_wcfg->ebm.logic_id, val, 6);
	unsigned char ret;
	ret= usercfg_set_sourceid(sourceid);
	if(ret){
		printf("sourceid restart network\n");
		ebm_cmdNet_restart();
	}
	return ret;
	
}

unsigned char ebmEnv_set_trumpet(unsigned char val)
{
#define usercfg_set_flgAmplifier(val) set_val_u8(val, &p_wcfg->flgAmplifier)      
	return usercfg_set_flgAmplifier(val);
}

unsigned char ebmEnv_get_trumpet(void)
{
	return p_readcfg->flgAmplifier;
}


unsigned char ebmEnv_get_register_1st_time(void)
{
	return 2;
}


unsigned char ebmEnv_get_phyid(unsigned char *id, unsigned char len)
{
	unsigned char ret=0;
	if(len == 6 && id != NULL){
		ret = ebmEnv_getphyid(id);
	}
	return ret;
}




